{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0b2b0acd",
   "metadata": {},
   "source": [
    "# lambda的基本使用方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cab9f0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 不需要想函数的名字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "364e9fe4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.<lambda>(x)>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 对比匿名函数与普通函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8092440",
   "metadata": {},
   "outputs": [],
   "source": [
    "- 普通函数：先定义def func()\n",
    "- 匿名函数：\n",
    "lambda 参数：表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d70c5550",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_func = lambda x: 2*x\n",
    "my_func(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ecca4455",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = lambda x: x**2\n",
    "a(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b260e2d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# map方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "268f7ccd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[(lambda x: 2*x)(i) for i in range(5)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "760ca23a",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 需要通过 list 转为列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8092e980",
   "metadata": {},
   "outputs": [],
   "source": [
    "list(map(lambda x: 2*x, range(5)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48b4ff01",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d304803",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdefb238",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f59c32b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35829bb2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d089287a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93f90502",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2a49cab",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55765797",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "593e7eac",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "122c08dd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfbc0ba6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db8b9113",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d4e9aa8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2050ceb2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc5e36f0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
